﻿using System;
using System.Collections.Generic;
using System.Linq;
using Core;
using Core.Domain.Customers;
using Core.Data;

namespace Services.Customers
{
    /// <summary>
    /// 简历服务实现类
    /// </summary>
    public partial class ResumeService : IResumeService
    {
        #region Fields

        private readonly IRepository<Resume> _resumeRepository;
        private readonly IRepository<WorkExperience> _workRepository;
        private readonly IRepository<Education> _educationRepository;

        #endregion

        #region Ctor

        public ResumeService(
            IRepository<Resume> resumeRepository,
            IRepository<WorkExperience> workRepository,
            IRepository<Education> educationRepository)
        {
            this._resumeRepository = resumeRepository;
            this._workRepository = workRepository;
            this._educationRepository = educationRepository;
        }

        #endregion

        #region 简历
        /// <summary>
        /// 删除简历
        /// </summary>
        /// <param name="entity"></param>
        public void DeleteResume(Resume entity)
        {
            if (entity == null)
                throw new ArgumentNullException("Resume");

            entity.Deleted = true;
            UpdateResume(entity);

        }

        /// <summary>
        /// 新增简历
        /// </summary>
        /// <param name="entity"></param>
        public void InsertResume(Resume entity)
        {
            if (entity == null)
                throw new ArgumentNullException("Resume");

            _resumeRepository.Insert(entity);
        }

        /// <summary>
        /// 更新简历
        /// </summary>
        /// <param name="entity"></param>
        public void UpdateResume(Resume entity)
        {
            if (entity == null)
                throw new ArgumentNullException("Resume");

            _resumeRepository.Update(entity);
        }

        /// <summary>
        /// 根据主键获取简历
        /// </summary>
        /// <param name="resumeId"></param>
        /// <returns></returns>
        public Resume GetResumeById(int resumeId)
        {
            if (resumeId == 0)
                return null;
            return _resumeRepository.GetById(resumeId);
        }

        /// <summary>
        /// 根据主键集合获取简历
        /// </summary>
        /// <param name="resumeIds"></param>
        /// <returns></returns>
        public IList<Resume> GetResumeByIds(int[] resumeIds)
        {
            if (resumeIds == null || resumeIds.Length == 0)
                return new List<Resume>();

            return _resumeRepository.Table.Where(e => resumeIds.Contains(e.Id)).ToList();
        }

        /// <summary>
        /// 筛选简历
        /// </summary>
        /// <returns></returns>
        public IPagedList<Resume> GetResumes(
            string name = "", int sex = -1, int workYear = 0, string phone = "", string mail = "",
            int cityId = 0, int workStateId = 0, int workCityId = 0,
            bool? deleted = null, bool? active = null,
            ResumeSortingEnum orderby = ResumeSortingEnum.DisplayOrderDown,
            int pageIndex = 0, int pageSize = int.MaxValue)
        {
            var query = _resumeRepository.Table;

            if (!String.IsNullOrWhiteSpace(name))
                query = query.Where(e => e.Name.Contains(name) || e.ResumeName.Contains(name));

            if (sex > -1)
                query = query.Where(e => sex == e.SexId);

            //if(workYear)

            if (!String.IsNullOrWhiteSpace(phone))
                query = query.Where(e => e.Phone.Contains(phone));

            if (!String.IsNullOrWhiteSpace(mail))
                query = query.Where(e => e.Email.Contains(mail));

            if (cityId > 0)
                query = query.Where(e => e.CityId == cityId);

            if (workStateId > 0)
                query = query.Where(e => e.WorkStateId == workStateId);

            if (workCityId > 0)
                query = query.Where(e => e.WorkStateId == workCityId);

            if (deleted.HasValue)
                query = query.Where(e => e.Deleted == deleted);

            if (active.HasValue)
                query = query.Where(e => e.Active == active);

            switch (orderby)
            {
                case ResumeSortingEnum.CreateDown:
                    query = query.OrderByDescending(e => e.CreateOn);
                    break;
                case ResumeSortingEnum.CreateUp:
                    query = query.OrderBy(e => e.CreateOn);
                    break;
                case ResumeSortingEnum.DisplayOrderDown:
                    query = query.OrderByDescending(e => e.UpdateOn);
                    break;
                case ResumeSortingEnum.DisplayOrderUp:
                    query = query.OrderBy(e => e.UpdateOn);
                    break;
                case ResumeSortingEnum.TimesDown:
                    query = query.OrderByDescending(e => e.ReadTimes);
                    break;
                case ResumeSortingEnum.TimesUp:
                    query = query.OrderBy(e => e.ReadTimes);
                    break;
                default:
                    query = query.OrderBy(e => e.UpdateOn);
                    break;
            }

            return new PagedList<Resume>(query, pageIndex, pageSize);
        }
        #endregion

        #region 教育背景

        /// <summary>
        /// 删除教育背景
        /// </summary>
        /// <param name="entity"></param>
        public void DeleteEducation(Education entity)
        {
            if (entity == null)
                throw new ArgumentNullException("Education");

            _educationRepository.Delete(entity);
        }

        /// <summary>
        /// 更新教育背景
        /// </summary>
        /// <param name="entity"></param>
        public void UpdateEducation(Education entity)
        {
            if (entity == null)
                throw new ArgumentNullException("Education");

            _educationRepository.Update(entity);
        }

        /// <summary>
        /// 插入教育背景
        /// </summary>
        /// <param name="entity"></param>
        public void InsertEducation(Education entity)
        {
            if (entity == null)
                throw new ArgumentNullException("Education");

            _educationRepository.Insert(entity);
        }

        /// <summary>
        /// 根据主键获取教育背景
        /// </summary>
        /// <param name="educationId"></param>
        /// <returns></returns>
        public Education GetEducationById(int educationId)
        {
            if (educationId == 0)
                return null;

            return _educationRepository.GetById(educationId);
        }

        /// <summary>
        /// 根据简历主键获取该简历所有的教育背景集合
        /// </summary>
        /// <param name="resumeId"></param>
        /// <returns></returns>
        public IList<Education> GetEducationByResumeId(int resumeId)
        {
            if (resumeId == 0)
                return new List<Education>();
            return _educationRepository.Table.Where(e => e.ResumeId == resumeId).ToList();
        }
        #endregion

        #region 工作经历

        /// <summary>
        /// 删除工作经历
        /// </summary>
        /// <param name="entity"></param>
        public void DeleteWorkExperience(WorkExperience entity)
        {
            if (entity == null)
                throw new ArgumentNullException("WorkExperience");

            _workRepository.Delete(entity);
        }

        /// <summary>
        /// 更新工作经历
        /// </summary>
        /// <param name="entity"></param>
        public void UpdateWorkExperience(WorkExperience entity)
        {
            if (entity == null)
                throw new ArgumentNullException("WorkExperience");

            _workRepository.Update(entity);
        }

        /// <summary>
        /// 新增工作经历
        /// </summary>
        /// <param name="entity"></param>
        public void InsertWorkExperience(WorkExperience entity)
        {
            if (entity == null)
                throw new ArgumentNullException("WorkExperience");

            _workRepository.Insert(entity);
        }

        /// <summary>
        /// 根据主键获取工作经历
        /// </summary>
        /// <param name="workExperienceId"></param>
        /// <returns></returns>
        public WorkExperience GetWorkExperienceById(int workExperienceId)
        {
            if (workExperienceId == 0)
                return null;
            return _workRepository.GetById(workExperienceId);
        }

        /// <summary>
        /// 根据简历主键获取该简历所有工作经历
        /// </summary>
        /// <param name="resumeId"></param>
        /// <returns></returns>
        public IList<WorkExperience> GetWorkExperienceByResumeId(int resumeId)
        {
            if (resumeId == 0)
                return new List<WorkExperience>();
            return _workRepository.Table.Where(e => e.ResumeId == resumeId).ToList();
        }
        #endregion
    }
}
